import goodsApi from '@/api/goods-management/goods'
import goodsSql from '@/sql/goods/index'
import { getToken } from '@/utils/token'
const state = {
  access_token: getToken(),
  orderAll: [],
  waitPayOrders: [],
  waitSendOrders: [],
  waitReceivedOrders: [],
  completedOrders: [],
  cancelOrders: []
}
const mutations = {
  SET_ORDERAll: (state, orderAll) => {
    state.orderAll = orderAll
  },
  SET_WAITSENDORDERS: (state, waitSendOrders) => {
    state.waitSendOrders = waitSendOrders
  },
  SET_WAITPAYORDERS: (state, waitPayOrders) => {
    state.waitPayOrders = waitPayOrders
  },
  SET_WAITRECEIVEDORDERS: (state, waitReceivedOrders) => {
    state.waitReceivedOrders = waitReceivedOrders
  },
  SET_WAITCOMPLETEDORDERS: (state, completedOrders) => {
    state.completedOrders = completedOrders
  },
  SET_WAITCANCELORDERS: (state, cancelOrders) => {
    state.cancelOrders = cancelOrders
  }
}
const actions = {
  // 获取列表的响应体
  promiseGetRequest({ state }, sql) {
    return new Promise((resolve, reject) => {
      goodsApi.get(state.access_token, sql).then(res => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 更新列表的响应体
  promiseUpdateRequest({ state }, sql) {
    return new Promise((resolve, reject) => {
      goodsApi.update(state.access_token, sql).then(res => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 添加数据的响应体
  promiseAddRequest({ state }, sql) {
    return new Promise((resolve, reject) => {
      goodsApi.add(state.access_token, sql).then(res => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 删除数据的响应体
  promiseDeleteRequest({ state }, sql) {
    return new Promise((resolve, reject) => {
      goodsApi.delete(state.access_token, sql).then(res => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取商品订单记录
  getOrderList({ state }) {
    const data = {
      requestName: 'getOrderListLinkOrderDetail'
    }
    return new Promise((resolve, reject) => {
      goodsApi.invokeCloudFunction(state.access_token, data).then(res => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  searchOrder({ state }, formData) {
    const data = {
      requestName: 'searchOrder',
      data: formData
    }
    return new Promise((resolve, reject) => {
      goodsApi.invokeCloudFunction(state.access_token, data).then(res => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取商品订单记录
  getOrderListByOrderStatus({ state }, order_status) {
    const data = {
      requestName: 'getOrderListLinkOrderDetailByOrderStatus',
      order_status: order_status
    }
    return new Promise((resolve, reject) => {
      goodsApi.invokeCloudFunction(state.access_token, data).then(res => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 修改订单状态
  updateOrderStatus({ state, dispatch }, formData) {
    goodsSql.recordSql.query = `db.collection("order").doc("${formData._id}").update(
      {
          data:{
            order_status: ${formData.order_status},
            update_time: new Date()
          }})`
    return dispatch('promiseUpdateRequest', goodsSql.recordSql)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
